package com.tz.dao;

import com.tz.entity.Category;
import com.tz.entity.Page;
import com.tz.entity.Picture;
import com.tz.entity.Product;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2016/7/25.
 */
@SuppressWarnings("ALL")
@Repository
@Transactional
public class ProductDaoImpl implements ProductDao {

    @Autowired
    private SessionFactory sf;

    private Session getSession(){
        return sf.getCurrentSession();
    }


    @Override
    public List<Product> findProductByCharacter(String character) {
        String hql = "from Product p where p.character=:character";
        Query q = getSession().createQuery(hql).setString("character", character);
        return q.list();
    }

    @Override
    public List<Product> findAllProducts() {
        return getSession().createQuery("from Product").list();
    }

    @Override
    public List<Picture> findPictureByProductId(long id) {
        return getSession().createQuery("from Picture where product_id=:productid").setLong("productid", id).list();
    }

    @Override
    public List<Category> findCategoryByCategoryId(long parentid) {
        return getSession().createQuery("from Category where parent=:parentid").setLong("parentid", parentid).list();
    }

    @Override
    public List<Product> findProductByCategoryId(long categoryid) {
        String hql = "from Product p where p.category=:category";
        Query q = getSession().createQuery(hql).setLong("category", categoryid);
        return q.list();
    }

    @Override
    public Product findProductById(long productid) {
        return (Product) getSession().get(Product.class, productid);
    }

    @Override
    public Page<Product> search(String categoryId, int pageNumber, String sortBy, String sortOrder, String viewType) {
        Page page = new Page();
        //pageSize,totalPage
        int pageSize = "listView".equals(viewType)? 6 : 9 ;
        int totalPage ;
        int totalCount;
        pageNumber = pageNumber == 0 ? 1 :pageNumber;
        //HQL语句
        String hql = "from Product p";
        //categoryId
        if(categoryId != null){
            hql += " where p.category="+categoryId;
        }
        //sortBy
        if("Product name A-Z".equals(sortBy)){
            hql += " order by p.name ";
        }else if("Product name Z-A".equals(sortBy)){
            hql += " order by p.name desc";
        }
        //sortOrder
        if(sortBy==null){
            if("Product Stock".equals(sortOrder)){
                hql += " order by p.stock";
            }else if("Price Lowest first".equals(sortOrder)){
                hql += " order by p.price";
            }
        }else{
            if("Product Stock".equals(sortOrder)){
                hql += ",p.stock";
            }else if("Price Lowest first".equals(sortOrder)){
                hql += ",p.price";
            }
        }

        List<Product> products = getSession().createQuery(hql)
                                            .setMaxResults(pageSize)
                                            .setFirstResult((pageNumber-1)*pageSize)
                                            .list();

        page.setData(products);
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);
        //获取totalCount
        if(null != categoryId){
            totalCount = this.findProductByCategoryId(Integer.parseInt(categoryId)).size();
        }else{
            totalCount = this.findAllProducts().size();
        }

        page.setTotalCount(totalCount);
        if(totalCount%pageSize != 0){
            if(totalCount < pageSize){
                totalPage = 1;
            }else{
                totalPage = totalCount / pageSize + 1;
            }
        }else{
            if(totalCount < pageSize){
                totalPage = 0;
            }else{
                totalPage = totalCount / pageSize;
            }
        }
        page.setTotalPage(totalPage);
        return page;
    }

}
